package 知识点.Object通用方法;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-12-17 22:12
 **/
public class TestHashCode {
    public static void main(String[] args) {
        A a1 = new A(1, 1);
        A a2 = new A(1, 1);
        Set<A> set1 = new HashSet<>();
        set1.add(a1);
        set1.add(a2);
        System.out.println(set1.size());

        B b1 = new B(1, 1);
        B b2 = new B(1, 1);
        Set<B> set2 = new HashSet<>();
        set2.add(b1);
        set2.add(b2);
        System.out.println(set2.size());
    }
}

class A{
    private int id;
    private int age;

    public A(int id, int age) {
        this.id = id;
        this.age = age;
    }
}

class B{
    private int id;
    private int age;

    public B(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        B b = (B) o;
        return id == b.id &&
                age == b.age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, age);
    }
}
